package com.example.alarmclockdemo

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProviders
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.fandou.airobot.R
import com.fandou.airobot.database.Alarm
import com.fandou.airobot.database.AlarmConstants
import com.fandou.airobot.database.AlarmDatabase
import com.fandou.airobot.ui.alarm.AlarmViewModel
import kotlinx.android.synthetic.main.alarm_clock_item.view.*
import java.text.DecimalFormat

/**
 * @author       Jm
 * @date         2020/6/24 16:52
 * @description  闹钟列表adapter
 */

class AlarmAdapter() : BaseQuickAdapter<Alarm, BaseViewHolder>(R.layout.alarm_clock_item) {

    /**
     * 开启状态下的文字颜色
     */
    private val mDefaultTextColor by lazy { Color.parseColor("#333333") }

    /**
     * 关闭状态下的文字颜色
     */
    private val mUnEnableTextColor by lazy { Color.parseColor("#76333333") }

    /**
     * 开启状态下的线条颜色
     */
    private val mDefaultLineColor by lazy { Color.parseColor("#76DDDDDD") }

    /**
     * 关闭状态下的线条颜色
     */
    private val mUnEnableLineColor by lazy { Color.parseColor("#DDDDDD") }


    private val mDecimalFormat: DecimalFormat by lazy { DecimalFormat("00") }

    /**
     * 是否在编辑状态
     */
    var mIsEditMode = false

    private val mViewModel: AlarmViewModel by lazy {
        ViewModelProviders.of(mContext as AppCompatActivity).get(
            AlarmViewModel::class.java
        )
    }


    @SuppressLint("SetTextI18n")
    override fun convert(helper: BaseViewHolder?, item: Alarm?) {

        helper!!.addOnClickListener(R.id.tvDelete)
        helper.addOnClickListener(R.id.tvEdit)

        helper.itemView.run {

            tvRemark.text = if (item!!.remark.isNullOrEmpty()) "" else item.remark

            val isSoundEnable = item.alertType == AlarmConstants.RemindType.ALERT_SOUND
            //如果当前闹钟或者倒计时是开启状态
            if (item.enable) {
                tv_time.setTextColor(mDefaultTextColor)
                tv_alarm_type.setTextColor(mDefaultTextColor)
                tvRemark.setTextColor(mDefaultTextColor)
                ivLine.setBackgroundColor(mDefaultLineColor)
                ivAlertType.setImageResource(if (isSoundEnable) R.drawable.ic_sound_on else R.drawable.ic_sound_off)


            } else {
                tv_time.setTextColor(mUnEnableTextColor)
                tv_alarm_type.setTextColor(mUnEnableTextColor)
                tvRemark.setTextColor(mUnEnableLineColor)
                ivLine.setBackgroundColor(mUnEnableLineColor)
                ivAlertType.setImageResource(if (isSoundEnable) R.drawable.ic_sound_on_unenable else R.drawable.ic_sound_off_unenable)
            }

            switchTurn.isChecked = item.enable
            switchTurn.setOnClickListener {
                item.enable = !item.enable

                if (!switchTurn.isChecked) {
                    countdown.stop()
                }
                notifyItemChanged(helper.adapterPosition)
                notifyItemChanged(helper.adapterPosition, data.size)

                //notify update for the list
                mViewModel.updateAlarmLiveData.value = item

                //update the database
                AlarmDatabase.getInstance().alarmDao().updateAlarm(item)
            }


            //重复闹钟
            if (item.getRepeat()) {
                tv_alarm_type.text = "重复闹钟"
            }

            //一次性闹钟
            else {
                tv_alarm_type.text = "一次性闹钟"
            }
            tv_time.text = "${mDecimalFormat.format(item.hour)}:${mDecimalFormat.format(item.minute)}"


            //下面含有倒计时的逻辑，现已经取消了倒计时功能，所以注释
            // 闹钟
//            if (item.alarmType == AlarmConstants.AlarmType.ALARM) {
//                tv_alarm_type.text = "定时闹钟"
//                tv_time.text = "${mDecimalFormat.format(item.hour)}:${mDecimalFormat.format(item.minute)}"
//
//                countdown.stop()
//                countdown.visibility = View.GONE
//
//
//                countdown.setOnCountdownEndListener(null)
//            }
//
//            //倒计时
//            else {
//                tv_alarm_type.text = "倒计时"
//                tv_time.text = "${mDecimalFormat.format(item.hour)}:${mDecimalFormat.format(item.minute)}:${mDecimalFormat.format(item.second)}"
//                // start the countdown
//
//
//                if (item.countdownTimestamp > 0 && item.enable) {
//                    countdown.start(item.countdownTimestamp)
//                    countdown.visibility = View.VISIBLE
//
//                } else {
//                    countdown.visibility = View.GONE
//                    switchTurn.isChecked = false
//                    item.enable = false
//                }
//
//                /**
//                 * 倒计时监听
//                 */
//                countdown.setOnCountdownEndListener {
//                    countdown.visibility = View.GONE
//                    item.enable = false
//                    switchTurn.isChecked = false
//
//                    notifyItemChanged(helper.adapterPosition)
//                    notifyItemChanged(helper.adapterPosition, data.size)
//
//                    //update the database
//                    AlarmDatabase.getInstance().alarmDao().updateAlarm(item)
//
////                    if (mAlertDialog == null) {
////                        mAlertDialog = XPopup.Builder(context).asCustom(AlarmRemindPop(context, "倒计时闹钟提醒！", item.remark)).show() as AlarmRemindPop
////                    } else {
////                        mAlertDialog!!.dismiss()
////                        mAlertDialog = XPopup.Builder(context).asCustom(AlarmRemindPop(context, "倒计时闹钟提醒！", item.remark)).show() as AlarmRemindPop
////                    }
//                }
//            }


            //编辑模式下，只有编辑时间 和  删除功能
            if (mIsEditMode) {
                switchTurn.visibility = View.GONE
                tvDelete.visibility = View.VISIBLE
                tvEdit.visibility = View.VISIBLE
                ivAlertType.visibility = View.INVISIBLE
                tvRemark.visibility = View.INVISIBLE
            } else {
                switchTurn.visibility = View.VISIBLE
                tvDelete.visibility = View.GONE
                tvEdit.visibility = View.GONE
                ivAlertType.visibility = View.VISIBLE
                tvRemark.visibility = View.VISIBLE
            }

        }
    }


    /**
     * delete an alarm by id
     */
    fun deleteAlarm(id: Long) {
        remove(data.indexOf(data.find { it.id == id }))
    }

    /**
     * Update the alarm
     */
    fun updateAlarm(alarm: Alarm) {
        setData(data.indexOf(data.find { it.id == alarm.id }), alarm)
    }
}